#coding=utf-8
from django.test import TestCase,Client
from blog.forms import BlogForm, CommentForm, EMPTY_TITLE_ERROR, EMPTY_BODY_ERROR, EMPTY_COMMENT_ERROR
from django.contrib.auth.models import User  
from blog.models import *
import unittest
import time

class Home_Test(TestCase):
    def test_home_render_html_template(self):
        response = self.client.get('http://localhost:8888/blog/login/')
        self.assertTemplateUsed(response, "base.html")

class LoginTest(TestCase):
    def test_login_response_code(self):
        response = self.client.get('http://localhost:8888/blog/login/')
        self.assertEqual(response.status_code, 200)
    
    def test_login_temp(self):
        response = self.client.get('http://localhost:8888/blog/login/')
        self.assertTemplateUsed(response, 'blog_login.html')
        
    def test_login_post_username_and_pwd_succ(self):
        response = self.client.post('http://localhost:8888/blog/login/', data={'username':'test','password':'qianru831104'})
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'blog_login.html')

class BlogFormTest(TestCase):        
    def test_add_blog_form(self):
        form = BlogForm()
        self.assertIn('placeholder="title"', form.as_p())
        self.assertIn('placeholder="body"', form.as_p())
#         self.fail(form.as_p())
    
    def test_form_validation_for_blank_title(self):
        form = BlogForm(data={'title':'','body':'abc'})
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors['title'], [EMPTY_TITLE_ERROR])
        
    def test_form_validation_for_blank_body(self):
        form = BlogForm(data={'title':'abc', 'body':''})
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors['body'], [EMPTY_BODY_ERROR])
        
    def test_form_save_blog(self):
#         timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
#         print timestamp
        user_ = User.objects.create(password='1qaz2wsx', is_superuser=False, username='guohuixin',is_staff=False, is_active=True)
        form = BlogForm(data={'title':"me", 'body':'abc'})
        new_item = form.save(for_user=user_)
#         print 'first:', Blog.objects.first()
#         print 'form:',new_item
        self.assertEqual(new_item, Blog.objects.first())
        self.assertEqual(new_item.title, 'me')
        self.assertEqual(new_item.author_id, 1)

    def test_form_validation_for_save_same_blog_title(self):
        user_ = User.objects.create(password='1qaz2wsx', is_superuser=False, username='guohuixin',is_staff=False, is_active=True)
        timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        type_ = Type.objects.create(type='blog')
        blog1 = BlogForm(data={'title':"me", 'body':'abc'})
        blog2 = Blog.objects.create(author=user_, title='me', body='abcd', timestamp=timestamp, clicktime=0, blogtype=type_)
        self.assertFalse(blog1.is_valid())
#         self.assertEqual(blog1.errors['title'], u'\u5177\u6709 \u6807\u9898 \u7684 \u535a\u5ba2 \u5df2\u5b58\u5728\u3002')
        
class CommentFormTest(TestCase): 
    def test_add_blog_form(self):
        form = CommentForm()
#         self.assertIn('placeholder="title"', form.as_p())
#         self.assertIn('placeholder="body"', form.as_p())
        self.fail(form.as_p())
    def test_form_validation_for_blank_content(self):
        form = CommentForm(data={'content':''}) 
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors['content'], [EMPTY_COMMENT_ERROR])
        
    def test_add_blog_comment(self):
        user_ = User.objects.create(password='1qaz2wsx', is_superuser=False, username='guohuixin',is_staff=False, is_active=True)
        timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        type_ = Type.objects.create(type='blog')
        blog_ = Blog.objects.create(author=user_, title='me', body='abcd', timestamp=timestamp,clicktime=1, blogtype=type_)
        commentform = CommentForm(data={'content':'SAFSDAF'})
        new_content = commentform.save(for_blog=blog_)
        self.assertEqual(new_content.id, 1)
        self.assertEqual(new_content.content, 'SAFSDAF')
        